﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using H2.Streams;
using H2.Streams.Sections;
using Mystery.SCNR;
using Mystery.SCNR.Scripts;

namespace Mystery
{
    public partial class ScriptViewer : Form
    {
        public MapStream Map { get; set; }
        public int ScnrOffset { get; set; }

        public ScriptViewer()
        {
            InitializeComponent();
        }

        public void LoadScripts()
        {
            lstFunctions.Items.Clear();

            foreach (ScriptFunction f in Script.Current.Functions)
                lstFunctions.Items.Add(f);//, f.Keep);

            foreach (BuiltInFunctions bif in Enum.GetValues(typeof(BuiltInFunctions)))
                cboSearchQuery.Items.Add(bif);

            lstFunctions.Sorted = true;
        }

        private void lstFunctions_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            //((ScriptFunction)lstFunctions.Items[e.Index]).Keep = e.NewValue == CheckState.Checked;
        }

        private void ScriptViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Visible = false;
            }
        }

        private void lstFunctions_SelectedValueChanged(object sender, EventArgs e)
        {
            if (lstFunctions.SelectedItem == null) return;
            trvDebug.Nodes.Clear();
            Script.Current.FillTreeView(trvDebug, (ScriptFunction)lstFunctions.SelectedItem);
        }

        private void cmdApplyNow_Click(object sender, EventArgs e)
        {
            //Update Scripts
            foreach (ScriptingSyntaxDatum datum in Script.Current.SyntaxDatums)
            {
                if (datum.Keep == false)
                {
                    datum.Previous.SetSibling(Script.Current.SyntaxDatums, datum.SiblingIndex, datum.SiblingIndexSalt);
                }
            }
            Map.ReOpen();
            ScriptingSyntaxDatum.Write(Map, ScnrOffset, Script.Current.SyntaxDatums);
            Map.Close();

            if (lstFunctions.SelectedItem == null) return;
            trvDebug.Nodes.Clear();
            Script.Current.FillTreeView(trvDebug, (ScriptFunction)lstFunctions.SelectedItem);
        }

        private void cboSearchQuery_TextChanged(object sender, EventArgs e)
        {
            lstFunctions.Items.Clear();

            foreach (ScriptFunction f in Script.Current.FindAllFunctions(cboSearchQuery.Text))
                lstFunctions.Items.Add(f);//, f.Keep);

            lstFunctions.Sorted = true;
        }

        private void trvDebug_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag == null) return;
            ((ScriptingSyntaxDatum)e.Node.Tag).Keep = e.Node.Checked;
        }
    }
}
